#!/bin/bash

# Audio File Export Script for Rivendell
#
#   (C) Copyright 2003-2005 Fred Gleason <fredg@paravelsystems.com>
#
#      $Id: rd_export_file,v 1.7 2007/05/10 04:38:48 daservis Exp $
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2 as
#   published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Usage:
#  rd_export_file [args]
#
# Argument Order:
#
#        <level> = Normalization level, expressed as linear ratio, 
#                  0 = No normalization
#    <in-format> = Input format, 0 = PCM16, 1 = MPEG Layer 2
#  <in-samprate> = Input sample rate, in samples/sec
#   <in-wavfile> = Source file for export.
#   <out-format> = Output format, 0 = PCM16, 1 = MPEG Layer 1
#                  2 = MPEG Layer 2, 3 = MPEG Layer 3, 4 = FLAC,
#                  5 = OggVorbis
#    <out-chans> = Output channels, 1 or 2
# <out-samprate> = Output sample rate, in samples/sec
#  <out-bitrate> = MPEG Bitrate, in kbps, or 0 for PCM16/VBR
#  <out-quality> = Quality (for VBR), 0 for fixed-rate or PCM16
#  <out-wavfile> = Destination file
#      <datfile> = Temporary file for peak level data.
#      <tmpfile> = Temporary file for intermediate audio

if [ "$#" -lt 10 ]; then
    cat <<EOF 
Usage: `basename $0` <level> <in-samprate> <in-wavfile> <out-format> <out-chans> <out-samprate> <out-bitrate> <out-wavfile> <datfile> <tmpfile>
    <level> = Normalization level, expressed as linear ratio (0 = No normalization)
    <in-format> = Input format, 0 = PCM16, 1 = MPEG-1 Layer 2
    <in-samprate> = Input sample rate, in samples/sec
    <in-wavfile> = Source file for import.
    <out-format> = Output format, 0 = PCM16, 2 = MPEG-1 Layer 2
    <out-chans> = Output channels, 1 or 2
    <out-samprate> = Output sample rate, in samples/sec
    <out-bitrate> = MPEG Bitrate, in kbps, or 0 for PCM16
    <out-quality> = Quality (for VBR)
    <out-wavfile> = Destination file for imported WAV data.
    <datfile> = Temporary file for peak level data.
    <tmpfile> = Temporary file for intermediate audio
EOF
    exit 1
fi

#
# Some sane variable names
#
NORMAL_LEVEL=${1}
FORMAT_IN=${2}
SAMPRATE_IN=${3}
FILE_IN=${4}
FORMAT_OUT=${5}
CHANS_OUT=${6}
SAMPRATE_OUT=${7}
BITRATE_OUT=${8}
QUALITY_OUT=${9}
FILE_OUT=${10}
PEAK=${11}
WORK=${12}

#
# Resampling Flag
#
if [ $SAMPRATE_IN != $SAMPRATE_OUT ] ; then
  RESAMPLE_FLAG="resample -q"
fi

#
# MPEG Stuff
#
if [ $CHANS_OUT = 1 ] ; then
  MPEG_MODE="m"
else
  MPEG_MODE="s"
fi
if [ $BITRATE_OUT = 0 ] ; then
  LAME_RATE="-q $QUALITY_OUT"
else
  LAME_RATE="-b $BITRATE_OUT"
fi
case "$SAMPRATE_OUT" in
  32000)
    TOOLAME_SAMPRATE=32
  ;;

  44100)
    TOOLAME_SAMPRATE=44.1
  ;;

  48000)
    TOOLAME_SAMPRATE=48
  ;;
esac

#
# Convert to PCM (if necessary)
#
if [ $FORMAT_IN = 0 ] ; then
  cp $FILE_IN $WORK
else
  mpg321 -q -w $WORK $FILE_IN
fi

#
# Get Peak Level
#
if [ $NORMAL_LEVEL != 0 ] ; then
  PEAK_LEVEL=`sox $WORK -e stat -v 2>&1 | grep -v ^sox` 
  SCALE=`echo "$NORMAL_LEVEL * $PEAK_LEVEL" | bc -l`
  if [ "$SCALE" == "1.00000000000000000000" ]; then
    SCALE="1"
  fi
  SOX_SCALE="-v $SCALE"
  chmod 644 $WORK
fi

case "$FORMAT_OUT" in
  0)  # PCM16
    sox $SOX_SCALE $WORK -r $SAMPRATE_OUT -c $CHANS_OUT "$FILE_OUT" $RESAMPLE_FLAG
  ;;

  1)  # MPEG Layer 1
    exit 1
  ;;

  2)  # Mpeg Layer 2
    sox $SOX_SCALE $WORK -r $SAMPRATE_OUT -c $CHANS_OUT -t wav - $RESAMPLE_FLAG | toolame -t 0 -s $TOOLAME_SAMPRATE -m $MPEG_MODE -b $BITRATE_OUT /dev/stdin "$FILE_OUT"
  ;;

  3)  # MPEG Layer 3
    sox $SOX_SCALE $WORK -r $SAMPRATE_OUT -c $CHANS_OUT -t raw - $RESAMPLE_FLAG | lame --silent -r -x -s $SAMPRATE_OUT -m $MPEG_MODE $LAME_RATE - "$FILE_OUT"
  ;;

  4)  # FLAC
    sox $SOX_SCALE $WORK -r $SAMPRATE_OUT -c $CHANS_OUT -t raw - $RESAMPLE_FLAG | flac -f --silent --best --endian=little --sign=signed --bps=16 --channels=$CHANS_OUT --sample-rate=$SAMPRATE_OUT --force-raw-format -o "$FILE_OUT" -
  ;;

  5)  # OggVorbis
    sox $SOX_SCALE $WORK -r $SAMPRATE_OUT -c $CHANS_OUT -t raw - $RESAMPLE_FLAG | oggenc -r -B 16 -C $CHANS_OUT -R $SAMPRATE_OUT -q $QUALITY_OUT -o "$FILE_OUT" -
  ;;
esac

rm -f $WORK
rm -f $PEAK

exit 0
